package com.tky.conference.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.tky.conference.R;
import com.tky.conference.base.BaseApplication;
import com.tky.conference.dao.ComeetingInfo;
import com.tky.conference.dao.DeptInfo;
import com.tky.conference.dao.HysInfo;
import com.tky.conference.dao.TreeInfo;
import com.tky.conference.entity.FailRequest;
import com.tky.conference.entity.LoginOutInfo;
import com.tky.conference.implInterfaces.DeleteImpl;
import com.tky.conference.implInterfaces.DeptInfoImpl;
import com.tky.conference.implInterfaces.HysInfoImpl;
import com.tky.conference.implInterfaces.TreeInfoImpl;
import com.tky.conference.interfaces.HttpResultListener;
import com.tky.conference.ui.AddActivity;
import com.tky.conference.ui.MyConferenceUpdateActivity;
import com.tky.conference.utlils.ShowDialog;
import com.tky.conference.utlils.ToastUtil;
import com.tky.conference.utlils.UIUtils;

import java.io.Serializable;
import java.util.List;

/**
 * Created by cll on 2015/12/15.
 */
public class MyConferenceAdapter extends BaseAdapter {
    private  Context context;
    private List<ComeetingInfo> datalist;
    private LoginOutInfo loginOutInfobj;
    private DeleteImpl deleteImpl;
    private TreeInfoImpl treeInfoImpl;
    private DeptInfoImpl deptInfoImpl;
    private HysInfoImpl hysInfoImpl;
    private Intent intent;
    boolean b1,b2,b3;
    private List<DeptInfo> deptInfoList;
    private List<HysInfo> hysInfoList;
    private List<TreeInfo> treeInfoList;
    public MyConferenceAdapter(Context context,List<ComeetingInfo> datalist){
        this.context=context;
        this.datalist=datalist;
    }

    @Override
    public int getCount() {
        Log.i("会议记录的长度",datalist.size()+"");
        return datalist.size();
    }

    @Override
    public Object getItem(int i) {
        return datalist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        final ComeetingInfo comeetingInfo=(datalist).get(i);

        if (view==null){
            view=View.inflate(context, R.layout.list_item,null);
            holder=new ViewHolder();
            holder.project= (TextView) view.findViewById(R.id.project);
            holder.destination= (TextView) view.findViewById(R.id.destination);
            holder.appoint= (TextView) view.findViewById(R.id.appoint);
            holder.time= (TextView) view.findViewById(R.id.time);
            holder.update=(Button)view.findViewById(R.id.alter);
            holder.delete=(Button)view.findViewById(R.id.delete);
            view.setTag(holder);
        }else {
            holder= (ViewHolder) view.getTag();
        }
        holder.project.setText(comeetingInfo.getHyzt());
        holder.destination.setText(comeetingInfo.getHydd());
        holder.appoint.setText(comeetingInfo.getStatus());
        holder.time.setText(comeetingInfo.getStime());

        holder.update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                b1=false;
                b2=false;
                b3=false;
                final SharedPreferences sharedPreferences=context.getSharedPreferences("save_account",Context.MODE_PRIVATE);
                String account=sharedPreferences.getString("account",null);
                intent=new Intent(BaseApplication.getContext(),MyConferenceUpdateActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("ComeetInfo", comeetingInfo);
                intent.putExtras(bundle);
//                UIUtils.startActivity(intent);
                //请求树结构
                TreeInfoImpl treeInfoImpl = new TreeInfoImpl(new HttpResultListener() {
                    @Override
                    public void onStartRequest() {
                    }

                    @Override
                    public void onResult(Object obj) {
                        if (obj instanceof FailRequest) {
                            if (!((FailRequest) obj).getStatus().equals(0)) {
                                ToastUtil.showToast(((Activity)context), ((FailRequest) obj).getMsg());
                            }
                        } else {

                            treeInfoList = (List<TreeInfo>) obj;
                            intent.putExtra("TREELIST", (Serializable) treeInfoList);
                            b1=true;
                            canGo();
                        }
                    }

                    @Override
                    public void onFinish() {

                    }
                });
                Log.i("account",account);
                treeInfoImpl.getTreeInfo(account);

                DeptInfoImpl deptInfoImpl = new DeptInfoImpl(new HttpResultListener() {
                    @Override
                    public void onStartRequest() {

                    }

                    @Override
                    public void onResult(Object obj) {
                        if (obj instanceof FailRequest) {
                            if (!((FailRequest) obj).getStatus().equals(0)) {
                                ToastUtil.showToast((Activity)context, ((FailRequest) obj).getMsg());
                            }
                        } else {
                            deptInfoList = (List<DeptInfo>) obj;
                            intent.putExtra("DEPTLIST", (Serializable) deptInfoList);
                            b2=true;
                            canGo();
                        }
                    }

                    @Override
                    public void onFinish() {

                    }
                });
                deptInfoImpl.getTreeConstruct(account);

                HysInfoImpl hysInfoImpl = new HysInfoImpl(new HttpResultListener() {
                    @Override
                    public void onStartRequest() {

                    }

                    @Override
                    public void onResult(Object obj) {
                        if (obj instanceof FailRequest) {
                            if (!((FailRequest) obj).getStatus().equals(0)) {
                                ToastUtil.showToast((Activity)context, ((FailRequest) obj).getMsg());
                            }
                        } else {
                            hysInfoList=(List<HysInfo>)obj;
                            intent.putExtra("HYSLIST", (Serializable) hysInfoList);
                            b3=true;
                            canGo();

                        }
                    }

                    @Override
                    public void onFinish() {

                    }
                });
                hysInfoImpl.hysInfo(account);
            }
        });

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder=new AlertDialog.Builder(context);
                builder.setTitle("删除")
                        .setMessage("确认要删除吗")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                deleteImpl = new DeleteImpl(new HttpResultListener() {
                                    @Override
                                    public void onStartRequest() {

                                    }

                                    @Override
                                    public void onResult(Object obj) {
                                        if (obj instanceof FailRequest) {
                                            if (!((FailRequest) obj).getStatus().equals(0)) {
                                                Toast.makeText(context, ((FailRequest) obj).getMsg(), Toast.LENGTH_SHORT).show();
                                            }
                                        }else {
                                            loginOutInfobj = (LoginOutInfo) obj;
                                            if(loginOutInfobj.getStatus().equals("0")){
                                                Log.i("删除事件响应。。。","okokok");
                                                datalist.remove(comeetingInfo);
                                                notifyDataSetChanged();
                                            }
                                        }
                                    }

                                    @Override
                                    public void onFinish() {

                                    }
                                });
                                Log.i("当前删除item的id",comeetingInfo.getId());
                                deleteImpl.delete(comeetingInfo.getId());
                            }
                        })
                        .setNegativeButton("取消",new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        }).show();
            }
        });
        return view;
    }


    class ViewHolder{
        TextView project;
        TextView time;
        TextView appoint;
        TextView destination;
        Button update;
        Button delete;
    }

    public void canGo(){
        if (b1&b2&b3){
            UIUtils.startActivity(intent);
        }
    }

}
